package com.atguigu.flink04;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;

/**
 * @author Felix
 * @date 2024/2/22
 * 该案例演示了将流中数据写到文件
 */
public class Flink08_Sink_File {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //开启检查点操作
        env.enableCheckpointing(5000L);

        DataGeneratorSource<String> dataGeneratorSource = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        return "num--" + value;
                    }
                },
                Long.MAX_VALUE,
                RateLimiterStrategy.perSecond(1000),
                Types.STRING
        );
        DataStreamSource<String> ds1
                = env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(), "data_source");

        //将流的数据写到文件中
        FileSink<String> sink = FileSink
                //指定输出文件的路径
                .forRowFormat(new Path("E:\\output\\"), new SimpleStringEncoder<String>("UTF-8"))
                .withOutputFileConfig(new OutputFileConfig("atguigu-",".log"))
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                //15s滚动一次
                                .withRolloverInterval(Duration.ofSeconds(15))
                                //上一条数据到来后，隔了指定的时间，还没有数据进来，进行滚动
                                .withInactivityInterval(Duration.ofSeconds(5))
                                //到了指定的大小进行滚动
                                .withMaxPartSize(new MemorySize(1024))
                                .build())
                .build();
        ds1.sinkTo(sink);

        env.execute();

    }
}
